<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends CI_Controller {
	var $gallery_path;
	function __construct(){
		parent::__construct();
		(!session_id()) ? session_start() : '';
		(isset($_SESSION['is_logged_in']) && $_SESSION['is_logged_in'] && isset($_SESSION['email']))? '':redirect('home');
		$this->load->model('users_model');
		$this->load->model('recipients_model');
		$this->load->model('messages_model');
		$this->load->model('posts_model');
		$this->load->helper('myuser_helper');
		$this->gallery_path = realpath(APPPATH . '../images');
	}

	public function index()
	{
	
		$data['records'] 		= $this->users_model->get_user();
		$data['title']			= "Sign in My Life My Eulogy";
		$data['main_content'] 	= 'profile/profile_view';
		$data['numPhotos'] 		= $this->messages_model->countMessages("photos");
		$data['numVideos'] 		= $this->messages_model->countMessages("videos");
		$data['numNotes'] 		= $this->messages_model->countMessages("notes");
		$data['numDocuments']	= $this->messages_model->countMessages("documents");
		$data['numJournal'] 	= $this->messages_model->countMessages("journal");
		$data['bottom_ads'] 	= Myuser_helper::getAds('bottom',1);
		
		$this->load->view('include/template', $data, array('error' => ''));
	}

	function remembrance_wall(){
		$data['title']= "My Life My Eulogy | Remembrance Wall";
		$data['main_content'] = 'profile/remembrance_wall_view';
		$data['records'] = $this->posts_model->get_remembrance_wall();
		$this->load->view('include/template', $data);
	}
	function get_days_per_month(){
		$output = '';
		$month = $this->input->post('month');
		$sel_id = ($this->input->post('id') == 'sel_birthday_month')? 'sel_birthday_day':'sel_death_day';
		$n_days = cal_days_in_month(CAL_GREGORIAN, $month, 1960);
		$output .= '<select name="'.$sel_id.'" id="'.$sel_id.'">';
			$output .= '<option value="">Day</option>';
				for($day=1;$day<=$n_days;$day++){
					$output .= '<option value="'.$day.'">'.$day.'</option>';
				}
		$output .= '</select>';
		echo $output;
	}//end fct
	function save_remembrance_wall(){
		$output = '';
		$post = $this->input->post();
		$birthday = $post['sel_birthday_year'].'-'.str_pad($post['sel_birthday_month'],2,"0",STR_PAD_LEFT).'-'.str_pad($post['sel_birthday_day'],2,"0",STR_PAD_LEFT);
		$date_of_death = $post['sel_death_year'].'-'.str_pad($post['sel_death_month'],2,"0",STR_PAD_LEFT).'-'.str_pad($post['sel_death_day'],2,"0",STR_PAD_LEFT);
		$c_date = date('Y-m-d H:i:s');
		$save = $this->posts_model->save_remembrance_wall($post,$c_date,$birthday,$date_of_death);
		if($save){
			$output .= '<div class="rem_msg_content">';
				$output .= '<div class="recipient-thumbs"></div>';
				$output .= '<div class="fleft">';
					$output .= '<div class ="rem_name">'.$post['uname'].'</div>';
					if(!empty($post['file_name'])):
						$output .= '<div><img src="'.base_url().'assets/images/wall/'.$post['file_name'].'.jpg" alt="message file"/></div>';
					endif;
					$output .= '<div class="rem_msg">'.$post['add-remwall-msg'].'</div>';
					$output .= '<div class="rem_posted">';
						$output .= '<div class="rem_p_icon fleft"></div>';
						$output .= '<div class="fleft rem_date">'.date('F j, Y, g:i a',strtotime($c_date)).'</div>';
						$output .= '<div class="clear"></div>';
					$output .= '</div>';
					$output .= '<div class="rem_posted">';
						$output .= '<div class="fleft">Posted to:</div>';
						$output .= '<div class="postedto_icon fleft"></div>';
						$output .= '<div class="f999 fleft">'.$post['add-remwall-name'].'</div>';
						$output .= '<div class="cal_icon fleft"></div>';
						$output .= '<div class="fleft">'.date('M d, Y',strtotime($birthday)).' - '.date('M d, Y',strtotime($date_of_death)).'</div>';
						$output .= '<div class="clear"></div>';
					$output .= '</div>';
				$output .= '</div>';
				$output .= '<div class="clear"></div>';
			$output .= '</div>';
		}else{
			$output .= '0';
		}
		echo $output;
	}

	function update_profile(){
		$post = $this->input->post();
		$query = $this->users_model->edit_profile($post);
		echo ($query)? 'true':'false';
		return $query;
	}
	function update_picture(){
		//print_r($_FILES);exit();
		$newfilename = md5($_SERVER['REMOTE_ADDR'].rand());
		$config = array(
			'allowed_types' => 'gif|jpg|png|bmp|jpeg',
			'upload_path' => $_SERVER['DOCUMENT_ROOT'].'/mlme/assets/images/useruploads/profile_photos/',
			'max_size' => 1000,
			'max_width' => 300,
			'max_height' => 300,
			'file_name' => $newfilename
		);
		$this->load->library('upload', $config);
		$this->upload->initialize($config);

		if ( ! $this->upload->do_upload('userpic')){
			$error = array('error' => $this->upload->display_errors());
			return $error;
		} else {
			$upload = array('upload_data' => $this->upload->data());
			$path = '/useruploads/profile_photos/'.$upload['upload_data']['file_name'];
			$this->users_model->edit_profile(array('picture'=>$path));
			redirect('profile');
		}
	}
	function logout()
	{
		session_destroy();
		redirect('home');
	}
}// end of controller







